using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;


namespace Pampa.WebControls
{
    [ToolboxData("<{0}:VerticalMenu runat=server></{0}:VerticalMenu>")]
    public class VerticalMenu : BaseControl
    {
        private bool scroll = false;
        private int scrollWidth = 15;

        private string pathImages = "Pampa.WebControls";

        private List<VerticalMenuItem> globalItems;
        private List<VerticalMenuSection> sections;

        public VerticalMenu()
        {
            globalItems = new List<VerticalMenuItem>();
            sections = new List<VerticalMenuSection>();

            this.CssClass = "menuDefault";
        }

        public string PathImages
        {
            get { return pathImages; }
            set { pathImages = value; }
        }
        
        public bool Scroll
        {
            get { return scroll; }
            set { scroll = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (this.CssClass == "menuDefault")
                RegisterStyleSheet("Pampa.WebControls/VerticalMenu.css");

            RegisterLibrary("Pampa.Common/Common.js");
            RegisterLibrary("Pampa.Common/Scroll.js");
            RegisterLibrary("Pampa.WebControls/VerticalMenu.js");
        }
        
        public VerticalMenuSection AddSection(string title)
        {
            VerticalMenuSection section = new VerticalMenuSection();
            section.Title = title;
            sections.Add(section);

            return section;
        }

        public VerticalMenuItem AddItem(string title, string link)
        {
            VerticalMenuItem item = new VerticalMenuItem();
            item.Title = title;
            item.Link = link;
            globalItems.Add(item);

            return item;
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (this.Scroll)
            {
                RenderScroll(writer);

                writer.AddStyleAttribute(HtmlTextWriterStyle.Position, "absolute");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Top, "0px");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Left, this.scrollWidth.ToString() + "px");
                writer.AddAttribute(HtmlTextWriterAttribute.Width, (this.Width.Value - this.scrollWidth).ToString() + "px");
                writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ID + "_scrollArea");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);

                RenderMainArea(writer);

                writer.RenderEndTag();
            }
            else
            {
                RenderMainArea(writer);
            }
        }

        private void RenderScroll(HtmlTextWriter writer)
        {
            writer.AddStyleAttribute(HtmlTextWriterStyle.Position, "absolute");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Top, "0px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Left, "0px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, this.Height.ToString());
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, this.scrollWidth.ToString() + "px");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.AddAttribute(HtmlTextWriterAttribute.Height, "100%");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            // up button
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "15px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "15px");
            writer.AddAttribute(HtmlTextWriterAttribute.Src, GetResourceUrl(this.PathImages) + "/scrollup.gif");
            writer.AddAttribute("srcon", GetResourceUrl(this.PathImages) + "/scrollup_on.gif");
            writer.AddAttribute("onmouseover", "Common.on(this); Scroll.scrollUp('" + this.ID + "', '" + this.ID + "_scrollArea')");
            writer.AddAttribute("onmouseout", "Common.off(this); Scroll.scrollEnd()");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();

            writer.RenderEndTag(); // td
            writer.RenderEndTag(); // tr

            writer.RenderBeginTag(HtmlTextWriterTag.Tr); 
            writer.AddAttribute(HtmlTextWriterAttribute.Valign, "bottom");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            // down button
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "15px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "15px");
            writer.AddAttribute(HtmlTextWriterAttribute.Src, GetResourceUrl(this.PathImages) + "/scrolldown.gif");
            writer.AddAttribute("srcon", GetResourceUrl(this.PathImages) + "/scrolldown_on.gif");
            writer.AddAttribute("onmouseover", "Common.on(this); Scroll.scrollDown('" + this.ID + "', '" + this.ID + "_scrollArea')");
            writer.AddAttribute("onmouseout", "Common.off(this); Scroll.scrollEnd()");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();

            writer.RenderEndTag(); // td
            writer.RenderEndTag(); // tr

            writer.RenderEndTag(); // table
            writer.RenderEndTag(); // div
        }

        private void RenderMainArea(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "100%");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "menuTable");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            // global items
            foreach (VerticalMenuItem item in globalItems)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "menuItemCell");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "menuItem");
                writer.AddAttribute(HtmlTextWriterAttribute.Href, "javascript:" + item.Link);
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write(item.Title);
                writer.RenderEndTag();
                writer.RenderEndTag();
                writer.RenderEndTag();
            }

            // secciones          
            foreach (VerticalMenuSection section in this.sections)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "section");
                writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                writer.Write(section.Title);
                writer.RenderEndTag();
                writer.RenderEndTag();

                foreach (VerticalMenuItem item in section.Items)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "menuItemCell");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "menuItem");
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "javascript:" + item.Link);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.Write(item.Title);
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                }
            }
        }
    }
}
